package com.bayihouse.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bayihouse.annotation.AuthCheck;
import com.bayihouse.common.BaseResponse;
import com.bayihouse.common.DeleteRequest;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.common.ResultUtils;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.exception.ThrowUtils;
import com.bayihouse.model.entity.Administer;
import com.bayihouse.model.entity.Announcement;
import com.bayihouse.model.request.announcement.AnnouncementAddRequest;
import com.bayihouse.model.request.announcement.AnnouncementQueryRequest;
import com.bayihouse.model.vo.AnnouncementDetailVO;
import com.bayihouse.model.vo.AnnouncementVO;
import com.bayihouse.service.*;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;


@RestController
@RequestMapping("/anncouncement")
@Slf4j
public class AnnouncementController {


    @Resource
    private AnnouncementService anncouncementService;


    // endregion

    // region 增删改查

    /**
     * 创建用户
     *
     * @param anncouncementAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Long> addAnnouncement(@RequestBody AnnouncementAddRequest anncouncementAddRequest, HttpServletRequest request) {
        if (anncouncementAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Gson gson = new Gson();
        HttpSession session = request.getSession();
        Announcement anncouncement = new Announcement();
        BeanUtils.copyProperties(anncouncementAddRequest, anncouncement);
        Administer administer = (Administer) session.getAttribute(AdministerConstant.Administer_LOGIN_STATE);
        anncouncement.setAdministerid(administer.getId());
        List<Long> fileidList = anncouncementAddRequest.getFileids();
        String json = gson.toJson(fileidList);
        anncouncement.setFileids(json);
        boolean result = anncouncementService.save(anncouncement);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(anncouncement.getId());

    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> deleteAnnouncement(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = anncouncementService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }


    /**
     * 根据 id 获取用户（仅管理员）
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<AnnouncementDetailVO> getAnnouncementById(long id, HttpServletRequest request) throws Exception {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Announcement anncouncement = anncouncementService.getById(id);
        ThrowUtils.throwIf(anncouncement == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(anncouncementService.getAnnouncementDetailVO(anncouncement));
    }

    public BaseResponse<Announcement> getAnnouncementById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Announcement anncouncement = anncouncementService.getById(id);
        ThrowUtils.throwIf(anncouncement == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(anncouncement);
    }

    /**
     * 根据 id 获取包装类
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<AnnouncementVO> getAnnouncementVOById(long id, HttpServletRequest request) throws Exception {
        BaseResponse<Announcement> response = getAnnouncementById(id);
        Announcement anncouncement = response.getData();
        return ResultUtils.success(anncouncementService.getAnnouncementVO(anncouncement));
    }

    /**
     * 分页获取用户列表（仅管理员）
     *
     * @param anncouncementQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<Announcement>> listAnnouncementByPage(@RequestBody AnnouncementQueryRequest anncouncementQueryRequest,
                                                                   HttpServletRequest request) {
        long current = anncouncementQueryRequest.getCurrent();
        long size = anncouncementQueryRequest.getPageSize();
        Page<Announcement> anncouncementPage = anncouncementService.page(new Page<>(current, size),
                anncouncementService.getQueryWrapper(anncouncementQueryRequest));
        return ResultUtils.success(anncouncementPage);
    }

    /**
     * 分页获取用户封装列表
     *
     * @param anncouncementQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<AnnouncementVO>> listAnnouncementVOByPage(@RequestBody AnnouncementQueryRequest anncouncementQueryRequest,
                                                                       HttpServletRequest request) {
        if (anncouncementQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = anncouncementQueryRequest.getCurrent();
        long size = anncouncementQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Announcement> anncouncementPage = anncouncementService.page(new Page<>(current, size),
                anncouncementService.getQueryWrapper(anncouncementQueryRequest));
        System.out.println(anncouncementPage.getRecords());
        Page<AnnouncementVO> anncouncementVOPage = new Page<>(current, size, anncouncementPage.getTotal());
        List<AnnouncementVO> anncouncementVOList = anncouncementService.getAnnouncementVOList(anncouncementPage.getRecords());
        anncouncementVOPage.setRecords(anncouncementVOList);
        System.out.println(anncouncementVOPage.getRecords());
        return ResultUtils.success(anncouncementVOPage);
    }


}




